<?php 

if (!empty($_POST)) die;

$auth = @$_SESSION['auth'];
$m_falter = @intval($_GET['m_falter']);
$digest_filter = @intval($_COOKIE['forum_topic_digest_filter']);
$page = @intval($_GET['page']);
$search = @filter_var($_GET['search'], FILTER_SANITIZE_STRING);

if (empty($page)) { $page = 1; }
$limit = 21;

//筛选器
$cond = "AND city = $forum_city";
$order_by = 'ORDER BY c_at DESC';

//筛选条件
switch ($m_falter) {
case 1: 			
	if (empty($auth)) to_login();
	$cond .= " AND id IN (SELECT topic_id FROM forum_topic_order WHERE user_id = ".$auth['id'].")";
	$v_title = '['.$auth['name'].'] 的订单'; 
	$v_p_url = '?c=forum&a=topic_my_list&m_falter=1&page=';
	break;
case 2: 			
	if (empty($auth)) to_login();
	$user_id = intval($_GET['user_id']);
	$cond .= " AND user_id = ".$auth['id']." AND id in (SELECT topic_id FROM forum_topic_order WHERE user_id = $user_id)";
	$v_title = '['.dt_query_one("SELECT name FROM user_info WHERE id = $user_id")['name'].'] 的订单'; 
	$v_p_url = '?c=forum&a=topic_my_list&m_falter=2&user_id='.$user_id.'&page=';
	$m_falter_e = '&user_id='.$user_id;
	break;
case 3: 			
	$v_title = '<img src="img/finance/hengxin_logo_sm_1.png" height="25px"> 新发贴 <small>>></small>'; 
	$v_p_url = '?c=forum&a=topic_my_list&m_falter=3&page=';
	break;
case 4: 			
	$order_by = 'ORDER BY today_up_c DESC';
	$v_title = '<img src="img/finance/hengxin_logo_sm_1.png" height="25px"> 最热贴 <small>>></small>'; 
	$v_p_url = '?c=forum&a=topic_my_list&m_falter=4&page=';
	break;
case 5: 			
	$order_by = 'ORDER BY order_c_s DESC';
	$v_title = '<img src="img/finance/hengxin_logo_sm_1.png" height="25px"> 最订贴 <small>>></small>'; 
	$v_p_url = '?c=forum&a=topic_my_list&m_falter=5&page=';
	break;
default:
	if (empty($auth)) to_login();
	$cond .= " AND user_id = ".$auth['id'];
	$v_title = '['.$auth['name'].'] 的帖子'; 
	$v_p_url = '?c=forum&a=topic_my_list&page=';
}

switch ($digest_filter) {
case 1:
	$cond .= " AND digest = 1";
	break;
}

$v_p_search = '';
if (!empty($search)) {
	$cond .= " AND title LIKE '%$search%'";
	$v_p_search = '&search='.$search;
}

if (!empty($cond)) $cond = preg_replace('/AND/', 'WHERE', $cond, 1);

$topics = dt_query("SELECT id, title, user_id, user_name, view_c, reply_c, l_r_user_id, l_r_user_name, l_r_at, digest, orders, order_c, pay, c_at FROM forum_topic $cond $order_by LIMIT ".$limit * ($page - 1).",$limit");
if (!$topics) die('获取数据失败！');

?>
<div class="container">
	<div class="row clearfix">
		<ol class="breadcrumb forum-breadcrumb">
			<li><a href="?c=forum"><span class="glyphicon glyphicon-home"></span> 首页</a></li>
			<li class="active"><?php echo $v_title ?></li>
		</ol>
	</div>
</div>
<div class="container forum-list-search">
	<div class="row clearfix">
		<div class="col-md-4 column">
		</div>
		<div class="col-md-4 column">
			<div class="input-group">
				<input id="topic_my_list_search" class="form-control" type="text" placeholder="搜索" value="<?php echo $search ?>">
				<a href="javascript:topic_my_list_search(<?php echo $digest_filter ?>, '<?php echo $m_falter.@$m_falter_e ?>')" class="input-group-addon btn"><span class="glyphicon glyphicon-search"></span> 搜索</a>
			</div>
		</div>
	</div>
</div>
<div class="container forum-topic-info">
	<div class="name">
		<h3><?php echo $v_title ?></h3>
	</div>
</div>
<div class="container">
	<div class="col-md-12 column forum-topic-list">
		<ul id="order_filter" class="nav nav-pills nav-pills-sm pull-right">
			<li id="3"><a href="?c=forum&a=topic_my_list&m_falter=3">时间</a></li>
			<li id="4"><a href="?c=forum&a=topic_my_list&m_falter=4">热度</a></li>                       
			<li id="5"><a href="?c=forum&a=topic_my_list&m_falter=5">订单</a></li>                       
		</ul>
		<ul id="digest_filter" class="nav nav-pills nav-pills-sm">
			<li><a href="javascript:do_digest_filter(0)">全部</a></li>
			<li><a href="javascript:do_digest_filter(1)">精华</a></li>                       
		</ul>
		<hr class="solid">
		<?php while($topic = mysql_fetch_array($topics)) { ?>
		<div class="forum-topic">
			<div class="num">
				<?php if (1 == $m_falter) { ?>
				<a id="do_topic_<?php echo $topic['id'] ?>_order" class="btn btn-default main-up" href="javascript:do_topic_order(<?php echo $topic['id'] ?>)"> 取消订单 </a>
				<?php } else if (empty($m_falter)) { ?>
				<?php if (0 != $topic['orders']) { ?>
				<a target="_blank" class="btn btn-default" href="?c=forum&a=topic_my_order&topic_id=<?php echo $topic['id'] ?>"> 查看订单 <?php if ($topic['order_c'] > 0) { ?><b class="label label-success"><?php echo $topic['order_c'] ?></b><?php } ?></a>
				<?php } ?>
				<?php } ?>
				<span>回复<em><?php echo $topic['reply_c'] ?></em></span>
				<span>浏览<em><?php echo $topic['view_c'] ?></em></span>
			</div>
			<div class="subject">
				<p class="title">
				<?php if (1 == $topic['digest']) { ?><span class="glyphicon glyphicon-fire"></span> <?php } else { ?><span class="glyphicon glyphicon-list-alt"></span> <?php } ?>
				<a target="_blank" href="?c=forum&a=topic_show&topic_id=<?php echo $topic['id'] ?>"><?php echo $topic['title'] ?></a>
				<?php if (0 < $topic['pay']) { ?>
				<span class="label label-danger">$ <?php echo intval($topic['pay']) ?></span>
				<?php } ?>
				</p>
				<p class="info">
				<a target="_blank" href="?c=center&user_id=<?php echo $topic['user_id'] ?>"><?php echo $topic['user_name'] ?></a><span><?php echo date("Y-m-d",$topic['c_at']) ?></span>
				最后回复：<a target="_blank" href="?c=center&user_id=<?php echo $topic['l_r_user_id'] ?>"><?php echo $topic['l_r_user_name'] ?></a><span><a target="_blank" href="?c=forum&a=to_topic_show_end&topic_id=<?php echo $topic['id'] ?>"><?php echo date("m-d H:i",$topic['l_r_at']) ?></a></span>
				</p>
			</div>
		</div>
		<?php } ?>
	</div>
</div>
<div class="container forum-pagination">
	<div class="row clearfix">
		<div class="col-md-10 column">
			<?php pagination('forum_topic', $cond, $page, $limit, $v_p_url, $v_p_search) ?>
		</div>
		<div class="col-md-2 column">
		</div>
	</div>
</div>
<script type="text/javascript">

$(document).ready(function(){
	$('li#drop_my').addClass('active');
	var digest_filter = ('' == getCookie('forum_topic_digest_filter')) ? 1 : getCookie('forum_topic_digest_filter');
	$('ul#digest_filter').find('li:eq('+digest_filter+')').addClass('active');
	$('ul#order_filter').find('li#<?php echo $m_falter ?>').addClass('active');
});

function do_digest_filter(mark) {
	setCookie('forum_topic_digest_filter', mark, 365, '/');
	location = '<?php echo $v_p_url.$v_p_search ?>';
}

</script>
